﻿using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using XTrace;

namespace XTrace_UnitTests
{
	[TestClass]
	public class UnitTest1
	{
		readonly XTraceSource _trace = new XTraceSource("testsource");

		[TestMethod]
		public void Should_rotate_structured_log_for_connected_listener()
		{
			// todo specify 1 day rotation
			// todo mock current date

			var sizeLimit = 10000;
			using (var sut = new StructuredTextFileXTraceListener("test", RelativePath.CurrentDir) { FilesCount = 100, SizeLimit = sizeLimit })
			{
				_trace.Information("test1");

				var file1_ = "test_1.xlog";
				Assert.IsTrue(File.Exists(file1_), "file 1 does not exists");
				var rowSize = new FileInfo(file1_).Length;

				_trace.Information("test2");
				Console.WriteLine(sizeLimit / rowSize);
				// overcapacity
				for (int i = 0; i < sizeLimit / rowSize; i++)
				{
					_trace.Information("test4");
				}
				_trace.Information("test3");
			}

			// verify 2 files
			var file1 = "test_1.xlog";
			var file2 = "test_2.xlog";
			Assert.IsTrue(File.Exists(file1), "file 1 does not exists");
			Assert.IsTrue(File.Exists(file2), "file 2 does not exists");
			var log1 = File.ReadAllText(file1);
			var log2 = File.ReadAllText(file2);
			StringAssert.Contains(log1, "test1");
			StringAssert.Contains(log1, "test2");
			StringAssert.DoesNotMatch(log1, new Regex("test3"));
			StringAssert.DoesNotMatch(log2, new Regex("test1"));
			StringAssert.DoesNotMatch(log2, new Regex("test2"));
			StringAssert.Contains(log2, "test3");
		}

	}
}
